/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.logger;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import de.rockon.fuzzy.controller.model.commands.ActionCommandPool;
import de.rockon.fuzzy.controller.util.Logger;
import de.rockon.fuzzy.controller.util.factories.IconFactory;
import de.rockon.fuzzy.controller.util.factories.UIFactory;

/**
 * Verwaltung des Log-Nachrichtenfensters
 * 
 */
public class LogMessage implements ActionListener {

	private JPanel content = null;

	private JCheckBox infoCheckBox = null;
	private JCheckBox errorCheckBox = null;
	private JCheckBox debugCheckBox = null;

	/**
	 * Baut das Log-Nachrichtenfenster
	 */
	public LogMessage() {
		JPanel content = new JPanel(new BorderLayout());
		JPanel statusBar = new JPanel(new BorderLayout());
		JToolBar checkBoxes = new JToolBar();
		checkBoxes.setFloatable(true);
		checkBoxes.setRollover(true);
		checkBoxes.setOrientation(SwingConstants.VERTICAL);

		JList list = new JList(Logger.getMessages());
		list.setCellRenderer(new LogMessageRenderer());
		list.setVisibleRowCount(4);
		statusBar.add(list, BorderLayout.NORTH);
		statusBar.add(new JScrollPane(list));
		Logger.register(list);

		infoCheckBox = UIFactory.getJCheckBox("", ActionCommandPool.LOGGER_INFO, this, Logger.isShowInfo());
		debugCheckBox = UIFactory.getJCheckBox("", ActionCommandPool.LOGGER_DEBUG, this, Logger.isShowDebug());
		errorCheckBox = UIFactory.getJCheckBox("", ActionCommandPool.LOGGER_ERROR, this, Logger.isShowError());

		infoCheckBox.setToolTipText("Info");
		debugCheckBox.setToolTipText("Debug");
		errorCheckBox.setToolTipText("Error");

		checkBoxes.add(infoCheckBox);
		checkBoxes.add(debugCheckBox);
		checkBoxes.add(errorCheckBox);

		setDebugIcon();
		setErrorIcon();
		setInfoIcon();

		content.add(statusBar, BorderLayout.CENTER);
		content.add(checkBoxes, BorderLayout.WEST);
		content.setBorder(new TitledBorder(new EtchedBorder(), "Log Messages"));

		this.content = content;
	}

	public void actionPerformed(ActionEvent e) {
		final String cmd = e.getActionCommand();
		if (cmd.equals(ActionCommandPool.LOGGER_DEBUG)) {
			Logger.toggleShowDebugFlag();
			setDebugIcon();
		} else if (cmd.equals(ActionCommandPool.LOGGER_ERROR)) {
			Logger.toggleShowErrorFlag();
			setErrorIcon();
		} else if (cmd.equals(ActionCommandPool.LOGGER_INFO)) {
			Logger.toggleShowInfoFlag();
			setInfoIcon();
		}
	}

	/**
	 * @return JPanel
	 */
	public JPanel getContent() {
		return content;
	}

	private void setDebugIcon() {
		if (Logger.isShowDebug()) {
			debugCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_DEBUG));
		} else {
			debugCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_NO_DEBUG));
		}
	}

	private void setErrorIcon() {
		if (Logger.isShowError()) {
			errorCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_ERROR));
		} else {
			errorCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_NO_ERROR));
		}
	}

	private void setInfoIcon() {
		if (Logger.isShowInfo()) {
			infoCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_INFO));
		} else {
			infoCheckBox.setIcon(IconFactory.getInstance().getImageIcon(IconFactory.ICON_NO_INFO));
		}
	}
}
